

cd "C:\Users\jeehoon\Dropbox\Poverty\Consumption Poverty"

use "C:\Users\jeehoon\Dropbox\Poverty\Consumption Poverty\all_cons_data_for_stata_80_17.dta", clear

* the data already contains some variables that I will construct in the do file.
drop male fam1 fam2 fam3 fam4 fam5 fam_male1 fam_male2 fam_male3 fam_male4 fam_male5 reg1 reg2 reg3 reg4 urban race1 race2 race3 race_type emp emp2 emp_type /*
*/ ed1 ed2 ed3 ed4 age_grp ownhome region_code dem_group* cons10 cons11 cons12 r_cons* 


********************************************************************************
*correct 1970s measures
drop if (renteq==. & cutenure<=3 & (ref_year==1972 | ref_year==1973))

gen double hflow2 = 0
replace hflow2 = renteq*12 if (cutenure<=3 & (ref_year==1972 | ref_year==1973))

replace cons5 = ((cons5*scale)+(hflow2/4)-(hflow/4))/scale if (ref_year==1972 | ref_year==1973)
drop if ref_year==2018
save inequality_data_70s_correct, replace 

***CREATE DEMOGRAPHIC VARIABLES FOR ELASTICITY REGRESSIONS
*create binary variable for gender
gen male=2-sex_ref
*Gen family type dummies
forvalues x=1/5 {
gen fam`x'=0
replace fam`x'=1 if ftype==`x'
gen fam_male`x'=fam`x'*male
}
*Create region dummies 
forvalues x=1/4 {
gen reg`x' =0
replace reg`x'=1 if region==`x'
replace reg`x'=0 if bls_urbn==2
}
gen urban=2-bls_urbn
*Create race dummies
gen race1=0
replace race1=1 if (ref_race==1 & hispanic==0)
gen race2=0
replace race2=1 if (ref_race==2 & hispanic==0)
gen race3=1-race1-race2
*Gen employment indicators 
gen emp=0 if (incweek1 != .)
replace emp=1 if (incweek1 !=. & incweek1>0)
gen emp2=0 if (incweek2 != .) 
replace emp2=1 if (incweek2 != . & incweek2>0)
gen emp_type=0
replace emp_type=1 if (emp==1 & emp2==1)
replace emp_type=2 if (emp==1 & emp2==0)
replace emp_type=3 if (emp==0 & emp2==1)
replace emp_type=4 if (emp==0 & emp2==0)
*Gen race indicator  
gen race_type=. 
replace race_type=1 if (race1==1)
replace race_type=2 if (race2==1)
replace race_type=3 if (race3==1)
*Gen education indicators and dummies (note, ed_type exists for earlier years, drop before creating new variable)
rename ed_type old_ed_type
gen ed_type=.
replace ed_type = old_ed_type if (ref_year<1970)
replace ed_type=1 if (ref_year>=1972 & ref_year<=1973 & (educ_ref==1 | educ_ref==2 | educ_ref==6))
replace ed_type=2 if (ref_year>=1972 & ref_year<=1973 & educ_ref==3)
replace ed_type=3 if (ref_year>=1972 & ref_year<=1973 & educ_ref==4)
replace ed_type=4 if (ref_year>=1972 & ref_year<=1973 & educ_ref==5)
replace ed_type=1 if (qyear>=801 & qyear<=955 & (educ_ref==1 | educ_ref==2 | educ_ref==7))
replace ed_type=2 if (qyear>=801 & qyear<=955 & educ_ref==3)
replace ed_type=3 if (qyear>=801 & qyear<=955 & educ_ref==4)
replace ed_type=4 if (qyear>=801 & qyear<=955 & (educ_ref==5 | educ_ref==6))
replace ed_type=1 if (qyear>955 & (educ_ref==0 | educ_ref==10 | educ_ref==11))
replace ed_type=2 if (qyear>955 & educ_ref==12)
replace ed_type=3 if (qyear>955 & (educ_ref>=13 & educ_ref<=14))
replace ed_type=4 if (qyear>955 & (educ_ref>=15 & educ_ref<=17))
forvalues x=1/4 {
gen ed`x'=0
replace ed`x'=1 if ed_type==`x'
}
*Gen age indicators 
gen age_grp=.
replace age_grp=1 if (age_ref>=0 & age_ref<=34)
replace age_grp=2 if (age_ref>=35 & age_ref<=49)
replace age_grp=3 if (age_ref>=50 & age_ref<=64)
replace age_grp=4 if (age_ref>=65)
*Gen home owner dummy
gen ownhome=.
replace ownhome=1 if (cutenure>=1 & cutenure<=3)
replace ownhome=0 if (cutenure>=4 & cutenure<=6)
gen region_code=region
replace region_code=5 if (bls_urbn==2) 
*Gen demographic groups 
gen dem_group2=10*ftype+emp_type
gen dem_group3=10*ftype+race_type
gen dem_group4=10*ftype+region_code
gen dem_group6=100*ftype+10*ed_type+race1
*Change dem_group6 into continuous variable 1/40
egen counter=group(dem_group6)
quietly {
forvalues x=1/40 {
gen grp_dum`x'=0
replace grp_dum`x'=1 if counter==`x'
}
}
*Create files for each year's merge
preserve 
keep if (ref_year==1961 | ref_year==1972 | ref_year==1973)
*drop observations that were dropped in predictions 
drop if houseval==99
save data_for_pred_merge60, replace
restore 

preserve 
keep if (ref_year>=1980 & ref_year<=1984) 
drop if ref_year==1979
save data_for_pred_merge80, replace 
restore 

*create new housing predictions for 1961 and 1980-81 
do qregressions_new_17.do 

*Create inequality figures for all years (NOT accounting for the new predicted 
*rent equivalent) This creates an Excel worksheet that has data for 10th, 50th, and 
*90th percentiles as well as 90/10, 50/10, and 90/50 ratios. There is also a 
*formatted Excel worksheet. The first sheet of data in the formatted worksheet
*is simply the data from the output of the following program. To change data,
*copy and past the first sheet from the program output into the first sheet of 
*the formatted version. 
do cons_inequality_data_61_17_MS

*create the same measures for years 1961, 1972-73, and 1980-81 
do cons_inequality_data_predhome_61_17_MS
